<?php

/*
* 圈子接口 
*/
namespace Api\Controller\V3;
use Think\Controller;
class ForumController extends CommonController{

    /**
      获取圈子的版块接口
    */
    public function get_parent_forum(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $DB = M('forum_parent_forum');

            $list = $DB->field('id,name')->select();

            /**
             * 获取版块下所属的圈子
             */
            foreach ($list as $key => $value) {
              # code...
              $reback = $this->_get_all_forum($value['id']);
              //合并数组
              $list2 = array_merge($list[$key],$reback);
            }

            
            // print_r($list2); die;

            if(!empty($list2)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list2);
                $res['content']= $list2;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'NULL';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }

    /**
     * 获取版块下面所有的圈子以及圈子内帖子数量
     */
    private function _get_all_forum($id){

        /**
         * 获取板块下的圈子
         */
        $res = M('forum_forum')->where(array('parentid' => $id,'invisible' => 0))->field('fid,name,short_description,threads,logo')->find();
        // print_r($res); die;
        $res['forum_name'] = $res['name'];
        unset($res['name']);

        if(!empty($res)){

            return $res;
        }else{


        }
    }




    /**
        获取圈子主页的数据接口
        fid圈子id
        rank 排序 1全部 2最新 3精华
     */
    public function get_forum_home_data(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $fid = I('get.fid');
            $rank = I('get.rank');
            $pagecount = I('get.pagecount');
            $userid = I('get.userid');
            //获取到圈子的详情信息
            $reback1 = $this->_get_forum_details_data($fid);

            /**
             * 获取圈子下面的特殊帖子
             */
            $reback2 = $this->_get_special_threads($fid);
            
            /**
             * 获取普通的帖子列表
             */
            $reback3 = $this->_get_ordinary_threads($fid,$rank,$pagecount);
            //对普通的文章截取前100个字作为简介
            foreach ($reback3 as $key => $value) {
              # code...
                $reback3[$key]['message'] = mb_substr($value['message'], 0,99,'utf-8');
            }

            /**
             * 对数组进行合并
             */
            $list['forum_detail'] = $reback1;
            $list['special_threads'] = $reback2;
            $list['ordinary_threads'] = $reback3;

            //圈子阅读数记录 更新+1
            $this->_add_read_forum_record($fid,$userid);

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content']= $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'NULL';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }



    /**
     * 得到圈子的详情
     */
    private function _get_forum_details_data($fid){

        $DB = M('forum_forum');
        $res1 = $DB->where(array('fid' => $fid,'invisible' =>0))->field('fid,name,logo,threads,posts,description')->find();

        //获取该圈子下所有管理员信息
        $res2 = M('forum_manager_relationship')->where(array('fid' => $res1['fid']))->getField('userid',true);
        $DB_users = M('forum_users');

        //循环获取管理员用户的信息
        foreach ($res2 as $key => $value) {
           # code...
           $res3[] = $DB_users->where(array('userid' => $value))->field('nickname,avatar')->find();
        }

        $res1['manager'] = $res3;

        if(!empty($res1)){

            return $res1;
        }
    }


    /**
     * 获取圈子的特殊帖子 置顶 推荐 热门之类
     */
    private function _get_special_threads($fid){

        $DB = M('forum_threads');

        $sql = "select tid,subject,issticktop,isdigest,ishot,
        isthumb,ishighlight from fd_forum_threads where issticktop=1 or isdigest=1 or ishot=1
         or isthumb=1 or ishighlight=1 and invisible=0 and fid=".$fid;

        $res = $DB->query($sql);

        // echo $DB->getLastsql(); die;
        if(!empty($res)){

            return $res;
        }
    }




    /**
     * 获取普通的文章列表
     */
    private function _get_ordinary_threads($fid,$rank,$pagecount){

        $DB = M('forum_threads');

        $pagenum = $pagecount*10;

        if($rank == 1){
            //全部
            $sql = "select fd_forum_threads.tid,fd_forum_threads.subject,fd_forum_threads.liketimes,fd_forum_threads.replies,fd_forum_threads.ctime,
            fd_forum_users.nickname,fd_forum_users.avatar,fd_forum_posts.message from fd_forum_threads left join fd_forum_users 
            on fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on fd_forum_threads.tid=fd_forum_posts.tid
             where fd_forum_threads.invisible=0 and fd_forum_threads.ishighlight=0 and fd_forum_threads.isdigest=0 and 
             fd_forum_threads.ishot=0 and fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0 and 
             fd_forum_users.invisible=0 and fd_forum_users.isban=0 and fd_forum_threads.fid=".$fid." order by fd_forum_threads.
             ctime desc limit ".$pagenum.",10";

        }else if($rank == 2){
            //最新
            $sql = "select fd_forum_threads.tid,fd_forum_threads.subject,fd_forum_threads.liketimes,fd_forum_threads.replies,fd_forum_threads.ctime,
            fd_forum_users.nickname,fd_forum_users.avatar,fd_forum_posts.message from fd_forum_threads left join fd_forum_users 
            on fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on fd_forum_threads.tid=fd_forum_posts.tid
             where fd_forum_threads.invisible=0 and fd_forum_threads.ishighlight=0 and fd_forum_threads.isdigest=0 and 
             fd_forum_threads.ishot=0 and fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0 and 
             fd_forum_users.invisible=0 and fd_forum_users.isban=0 and fd_forum_threads.fid=".$fid." order by fd_forum_threads.
             ctime desc limit ".$pagenum.",10";
        }else if ($rank == 3) {
            //精华
            $sql = "select fd_forum_threads.tid,fd_forum_threads.subject,fd_forum_threads.liketimes,fd_forum_threads.replies,fd_forum_threads.ctime,
            fd_forum_users.nickname,fd_forum_users.avatar,fd_forum_posts.message from fd_forum_threads left join fd_forum_users 
            on fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on fd_forum_threads.tid=fd_forum_posts.tid
             where fd_forum_threads.invisible=0 and fd_forum_threads.ishighlight=0 and fd_forum_threads.isdigest=1 and 
             fd_forum_threads.ishot=0 and fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0 and 
             fd_forum_users.invisible=0 and fd_forum_users.isban=0 and fd_forum_threads.fid=".$fid." order by fd_forum_threads.
             ctime desc limit ".$pagenum.",10";
        }

        
         $res = $DB->query($sql);

         if(!empty($res)){

            return $res;
         }
    }






    /**
        获取帖子的详情
     */
    public function get_one_posts_details(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $tid = I('get.tid');
            $userid = I('get.userid');
            //首先获取用户的部分个人信息 以及文章的信息
            $reback1 = $this->_get_user_and_threads_info($tid,$userid);
            //获取帖子的图片列表
            $posts_img_arr = explode(',', $reback1['images']);
            $DB = M('forum_images');
            foreach ($posts_img_arr as $key => $value) {
              # code...
                $reback1['images'][$key] = $DB->where(array('id' => $value))->getField('url');
            }


            //获取10评论信息
            // $reback2 = $this->_get_ten_posts($tid);

            $list['first_posts'] = $reback1;
            // $list['posts'] = $reback2; 

            //主题的阅读次数添加记录并+1
            $this->_add_threads_read_record($tid,$userid);

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content']= $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'NULL';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }


    /**
     * 获取用户的部分信息以及主题帖子的信息
     */
    private function _get_user_and_threads_info($tid,$userid){

        $DB = M('forum_threads');
        //获取主题帖的作者头像等级主题帖标题内容
        $sql = "select fd_forum_threads.subject,fd_forum_threads.views,fd_forum_threads.liketimes,fd_forum_threads.replies,
        fd_forum_threads.ctime,fd_forum_posts.images,
        fd_forum_posts.message, fd_forum_users.userid,fd_forum_users.avatar,fd_forum_users.level,fd_forum_users.nickname 
        from fd_forum_threads left join fd_forum_posts on fd_forum_threads.tid=fd_forum_posts.tid left join fd_forum_users 
        on fd_forum_threads.userid=fd_forum_users.userid where fd_forum_users.invisible=0 and fd_forum_users.isban=0 and 
        fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0 and fd_forum_threads.tid=".$tid;

        $res = $DB->query($sql);
        
        $res = $res[0];

        //判断用户是否已经对文章进行点赞
        //判断用户是否收藏了该帖子
        if(!empty($userid)){

            $isthreadslike = M('forum_threads_comment_like_record')->where(array('tid' => $tid,'userid' => $userid))->find();
            $isthreadscollection = M('forum_threads_collection_record')->where(array('tid' => $tid,'userid' => $userid))->find();
        }else{

            $isthreadslike = 0;
            $isthreadscollection = 0;
        }

        $res['isthreadslike'] = $isthreadslike;
        $res['isthreadscollection'] = $isthreadscollection;
        // print_r($res); die;
        if(!empty($res)){

            return $res;
        }
    }


    /**
     * 获取主题的十条评论（帖子）
     */
    private function _get_ten_posts($tid){

        $DB = M('forum_posts');

        $sql = "select fd_forum_posts.pid,fd_forum_posts.userid,fd_forum_posts.message,fd_forum_posts.ctime,fd_forum_users.
        nickname,fd_forum_users.avatar,fd_forum_users.level from fd_forum_posts left join fd_forum_users on 
        fd_forum_posts.userid=fd_forum_users.userid where fd_forum_posts.anonymous=0 and fd_forum_posts.invisible=0 and
        fd_forum_posts.isfirst=0 and fd_forum_posts.reply_pid=0 and fd_forum_posts.tid=".$tid." order by fd_forum_posts.
        ctime desc limit 0,10";
        $res = $DB->query($sql);
        // echo $DB->getLastsql(); die;
        //获取帖子的被评论次数 被点赞次数  二级帖
        
        $DB_1 = M('forum_posts_comment_like_record');
        $DB_2 = M('forum_posts_comment_record');

        foreach ($res as $key => $value) {
          # code...
            $res[$key]['comment_count'] = $DB_1->where(array('pid' => $value['pid'],'userid' => $value['userid']))->count();
            $res[$key]['like_count'] = $DB_2->where(array('pid' => $value['pid'],'userid' => $value['userid']))->count();

            $sql_1 = "select fd_forum_posts.message,fd_forum_users.nickname from fd_forum_posts left join fd_forum_users
             on fd_forum_posts.userid=fd_forum_users.userid where fd_forum_posts.reply_pid=".$value['pid']." and
              fd_forum_posts.invisible=0"; 
            // $res[$key]['child_posts'] = $DB->where(array('reply_pid' => $value['pid'],'invisible' => 0))->find();
            $res[$key]['child_posts'] = $DB->query($sql_1);
        }

        if(!empty($res)){

            return $res;
        }

    }



    /**
        图片上传接口
     */
    public function upload_image_posts() {
        if(IS_POST){

            $mid = I('post.mid');
            $password=I('post.password');
            $chk_user = $this->chk_user($mid, $password);

            if($chk_user){

                $url='/Uploads'.'/'.$this->upload($_FILES['image']);
                $DB = M("forum_images");
                $data['url']=$url;
                $id = $DB->add($data);

            }else{

            }

            $this->json_output($id);

        }
    }


    /**
     * 图片上传方法
     */
    private function upload($file)
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     5242880 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =      'forum/'; // 设置附件上传目录
        // 上传文件 
        $info   =   $upload->uploadOne($file);
        if(!$info)
        {
            // 上传错误提示错误信息    
            $this->error($upload->getError());
        }
        else
        {
            // 上传成功 获取上传文件信息    
            return $info['savepath'].$info['savename'];    
        }
    }



    /**
         发帖接口
     */
    
    public function publish_one_posts(){

        $mid = I('post.mid');
        $password=I('post.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('post.userid');
            $subject = I('post.subject');
            $message = I('post.content');
            $images = I('post.images');
            $fid = I('post.fid');

            //检测发表的主题帖子内容是否有敏感字
            $this->chk_bad_words($subject);
            $this->chk_bad_words($message);

            //箱数据表添加新的主题获得tid
            $tid = $this->_add_new_forum_threads($userid,$subject,$fid);

            //向数据表添加新的帖子记录
            if(!empty($tid)){

                $reback = $this->_add_new_forum_posts($fid,$tid,$userid,$message,$images);

                //发帖成功后用户获得积分
                $this->credit_system('publish_threads',$userid);
            }

            if(!empty($reback)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = 0;
                $res['content']= "";
            }else{

                $res['err'] = 1;
                $res['msg'] = '发布失败';
                $res['count'] = 0;
                $res['content']= 0;
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
        
    }


    /**
     * 添加新的主题
     */
    private function _add_new_forum_threads($userid,$subject,$fid){

        $DB = M('forum_threads');
        $data['userid'] = $userid;
        $data['fid'] = $fid;
        $data['subject'] = $subject;
        $data['ctime'] = I('server.REQUEST_TIME');

        $tid = $DB->data($data)->add();

        if(!empty($tid)){

            return $tid;
        }
    }


    /**
     * 向数据表写入新的帖子记录
     */
    private function _add_new_forum_posts($fid,$tid,$userid,$message,$images){

        $DB = M('forum_posts ');
        $data['fid'] = $fid;
        $data['tid'] = $tid;
        $data['isfirst'] = 1;
        $data['userid'] = $userid;
        $data['message'] = $message;
        $data['images'] = $images;
        $data['ctime'] = I('server.REQUEST_TIME');
        $data['floor_rank'] = 0;

        $pid = $DB->data($data)->add();

        if(!empty($pid)){

            return $pid;
        }
    }





    /**
      回复帖子接口
    **/
    public function reply_one_posts(){

        $mid = I('post.mid');
        $password=I('post.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $data['reply_pid'] = I('post.pid');
            $data['fid'] = I('post.fid');
            $data['tid'] = I('post.tid');
            $data['isfirst'] = 0;
            $data['userid'] = I('post.userid');
            $data['message'] = I('post.content');
            $data['ctime'] = I('server.REQUEST_TIME');
            $data['images'] = I('post.images');

            //检测发表的主题帖子内容是否有敏感字
            $this->chk_bad_words($data['message']);

            //获取最新楼层数
            $max_floor = $DB->where(array('tid' => $data['tid']))->count();
            $data['floor_rank'] = $max_floor;

            $DB = M('forum_posts');

            $result = $DB->data($data)->add();

            //回帖成功后用户获得积分
            $this->credit_system('reply',$userid);

            //记录回帖
            $this->_add_posts_comment_record($data['reply_pid'],$data['userid']);
            //记录主题评论
            $this->_add_threads_comment_record($data['tid'],$data['userid']);

            if(!empty($result)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = "";
                $res['content']= "";
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);
    }



    /**
      获取主题内部的回复帖子 pagecount
     */
    public function get_reply_posts_by_page(){


        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $tid = I('get.tid');
            $pagecount = I('get.pagecount');

            $pagenum = $pagecount*10;

            $DB = M('forum_posts');

            $sql = "select fd_forum_posts.pid,fd_forum_posts.reply_pid,fd_forum_posts.floor_rank,fd_forum_posts.userid,fd_forum_posts.message,fd_forum_posts.ctime,
            fd_forum_users.nickname,fd_forum_users.avatar,fd_forum_users.level from fd_forum_posts left join fd_forum_users on 
            fd_forum_posts.userid=fd_forum_users.userid where fd_forum_posts.invisible=0 and
            fd_forum_posts.isfirst=0 and fd_forum_posts.tid=".$tid." order by fd_forum_posts.
            ctime asc limit ".$pagenum.",10";
            $list = $DB->query($sql);

            //获取帖子的被评论次数 被点赞次数  引用评论
            $DB_1 = M('forum_posts_comment_like_record');
            $DB_2 = M('forum_posts_comment_record');
            // print_r($list); die;
            foreach ($list as $key => $value) {
              # code...
                $list[$key]['comment_count'] = $DB_1->where(array('pid' => $value['pid'],'userid' => $value['userid']))->count();
                $list[$key]['like_count'] = $DB_2->where(array('pid' => $value['pid'],'userid' => $value['userid']))->count();

                $sql_1 = "select fd_forum_posts.message,fd_forum_users.nickname from fd_forum_posts left join fd_forum_users
                 on fd_forum_posts.userid=fd_forum_users.userid where fd_forum_posts.pid=".$value['reply_pid']." and
                  fd_forum_posts.invisible=0"; 

                $list[$key]['cite_posts'] = $DB->query($sql_1);
                switch ($list[$key]['floor_rank']) {
                    case 0:
                        # code...
                        $list[$key]['floor_rank'] = C("floor1");
                        break;
                    case 1:
                        # code...
                        $list[$key]['floor_rank'] = C("floor2");
                        break;
                    case 2:
                        # code...
                        $list[$key]['floor_rank'] = C("floor3");
                        break;
                    default:
                        $list[$key]['floor_rank'] = $value['floor_rank']."楼";
                        break;
                }
            }


            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }



    /**
        只看楼主的回帖
     */
    public function get_lord_reply_posts_by_page(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $tid = I('get.tid');
            $pagecount = I('get.pagecount');
            $userid = I('get.userid');
            $pagenum = $pagecount*10;

            $DB = M('forum_posts');

            $sql = "select fd_forum_posts.pid,fd_forum_posts.reply_pid,fd_forum_posts.floor_rank,fd_forum_posts.userid,fd_forum_posts.message,fd_forum_posts.ctime,
            fd_forum_users.nickname,fd_forum_users.avatar,fd_forum_users.level from fd_forum_posts left join fd_forum_users on 
            fd_forum_posts.userid=fd_forum_users.userid where fd_forum_posts.invisible=0 and
            fd_forum_posts.isfirst=0 and fd_forum_posts.tid=".$tid." and fd_forum_posts.userid=".$userid." order by fd_forum_posts.
            ctime asc limit ".$pagenum.",10";
            $list = $DB->query($sql);

            //获取帖子的被评论次数 被点赞次数  引用评论
            $DB_1 = M('forum_posts_comment_like_record');
            $DB_2 = M('forum_posts_comment_record');
            // print_r($list); die;
            foreach ($list as $key => $value) {
              # code...
                $list[$key]['comment_count'] = $DB_1->where(array('pid' => $value['pid'],'userid' => $value['userid']))->count();
                $list[$key]['like_count'] = $DB_2->where(array('pid' => $value['pid'],'userid' => $value['userid']))->count();

                $sql_1 = "select fd_forum_posts.message,fd_forum_users.nickname from fd_forum_posts left join fd_forum_users
                 on fd_forum_posts.userid=fd_forum_users.userid where fd_forum_posts.reply_pid=".$value['pid']." and
                  fd_forum_posts.invisible=0"; 

                $list[$key]['cite_posts'] = $DB->query($sql_1);
                $list[$key]['floor_rank'] = $value['floor_rank']."楼";

            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }


    /**
     用户的个人主页
     */
    public function get_user_home_info(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('get.userid');
            $pasvuserid = I('get.pasvuserid');

            $DB_1 = M('forum_users');
            //获取用户的信息
            $list = $DB_1->where(array('userid' => $pasvuserid))->field('userid,nickname,avatar,level,followedcount,followcount')->find();

            //判断用户是否已经关注该用户
            //pasvuserid 被关注用户(被查看主页的用户)  userid行为用户(查看主页的用户)
            $list['follow_status'] = $this->_chk_whether_follow_user($pasvuserid,$userid);

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }



    /**
     * 查看用户是否关注
     */
    private function _chk_whether_follow_user($touserid,$fromuserid){

        $DB = M('forum_follow_user');

        $result = $DB->where(array('touserid' => $touserid,'fromuserid' => $fromuserid))->find();

        if(empty($result)){
            //表内没有记录
            $follow = 0;
        }else{

            if($result['follow_status'] == 0){
                //取消关注
                $follow = 0;
            }else{
                //关注
                $follow = 1;
            }
        }

        return $follow;
    }



    /**
     点赞接口 
     */
    public function set_comment_like(){


        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('get.userid');
            $type = I('get.type');
            if($type == 1){
                //如果type=1 对主题进行点赞
                $tid = I('get.tid');
                $reback = $this->_set_comment_like($userid,$tid,0);
            }else if($type == 2){
                //如果type=2对回帖进行点赞
                // echo 123; die;
                $pid = I('get.pid');
                $reback = $this->_set_comment_like($userid,0,$pid);
            }
            //点赞成功后
            $this->credit_system('like',$userid);
            if(!empty($reback)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = 0;
                $res['content'] = "";
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }
        

        echo json_encode($res);
    }



    /**
     * 点赞记录表
     */
    private function _set_comment_like($userid,$tid,$pid){

        if($pid == 0){
            //对主题进行点赞
            $data['userid'] = $userid;
            $data['tid'] = $tid;
            $data['ctime'] = I('server.REQUEST_TIME');

            $result = M('forum_threads_comment_like_record')->data($data)->add();

            //主题点赞liketimes+1
            M('forum_threads')->where(array('tid' => $tid))->setInc('liketimes');

        }else if($tid == 0){
            //对回帖进行点赞
            $data['userid'] = $userid;
            $data['pid'] = $pid;
            $data['ctime'] = I('server.REQUEST_TIME');
            // print_r($data); die;
            $result = M('forum_posts_comment_like_record')->data($data)->add();
            //帖子点赞liketimes+1
            M('forum_posts')->where(array('pid' => $pid))->setInc('liketimes');
        }

        return $result;
    }



    /**
     收藏接口
     */
    public function set_threads_collection(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('get.userid');
            $tid = I('get.tid');
            $iscollection = I('get.iscollection');

            $DB = M('forum_threads_collection_record');

            $reback = $DB->where(array('tid' => $tid,'userid' => $userid))->getField('id');

            if(!empty($reback)){
                //有收藏记录
                $data['iscollection'] = $iscollection;
                $result = $DB->where(array('id' => $reback))->data($data)->save();
                
            }else{
                //没有收藏记录
                $data['userid'] = $userid;
                $data['tid'] = $tid;
                $data['iscollection'] = 1;
                $data['ctime'] = I('server.REQUEST_TIME');
                $result = $DB->data($data)->add();
            }

            //对主题的收藏人数进行修改
            if($follow_status == 1){
                //收藏主题 主题收藏数+1
                M('forum_threads')->where(array('tid' => $tid))->setInc('favtimes');

            }else if($follow_status == 0){
                //取消收藏 主题收藏数-1
                M('forum_threads')->where(array('tid' => $tid))->setDec('favtimes');
            }


            if(!empty($result)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = 0;
                $res['content'] = "";
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }
        

        echo json_encode($res);
    }



    /**
      关注接口
     */
    public function set_follow_user(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $touserid = I('get.pasvuserid');
            $fromuserid = I('get.userid');
            $follow_status = I('get.follow_status');

            $DB = M('forum_follow_user');

            //首先查看是否已有关注记录
            $result = $DB->where(array('touserid' => $touserid,'fromuserid' => $fromuserid))->find();

            if(!empty($result)){
                //已经有关注记录
                $data['follow_status'] = $follow_status;
                $data['utime'] = I('server.REQUEST_TIME');
                $reback = $DB->where(array('fromuserid' => $fromuserid,'touserid' => $touserid))->data($data)->save();
            }else{
                //没有关注记录
                $data['touserid'] = $touserid;
                $data['fromuserid'] = $fromuserid;
                $data['ctime'] = I('server.REQUEST_TIME');
                $data['follow_status'] = $follow_status;
                $reback = $DB->data($data)->add();
            }
            //对用户的关注人数进行修改
            if($follow_status == 1){
                //关注用户 被关注用户粉丝+1  关注用户关注数+1
                M('forum_users')->where(array('userid' => $touserid))->setInc('followedcount');
                M('forum_users')->where(array('userid' => $fromuserid))->setInc('followcount');

                //关注成功后 关注用户加分
                $this->credit_system('follow_live',$fromuserid);
                //关注成功后 被关注用户加分
                $this->credit_system('fan_live',$fromuserid);

            }else if($follow_status == 0){
                //取消关注  被关注用户粉丝-1   关注用户关注数-1
                M('forum_users')->where(array('userid' => $touserid))->setDec('followedcount');
                M('forum_users')->where(array('userid' => $fromuserid))->setDec('followcount');

                //取消关注扣分
                $this->credit_system('follow_die',$fromuserid);
                //流失粉丝扣分
                $this->credit_system('fan_die',$fromuserid);
            }

            if(!empty($reback)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = 0;
                $res['content'] = "";
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }



    /**
      分享接口
     */
    public function threads_share(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $tid = I('tid');
            $userid = I('get.userid');
            $DB_1 = M('forum_threads');
            $DB_2 = M('forum_threads_share_record');
            //获取要分享的主题的资料
            $sql = "select fd_forum_threads.*,fd_forum_posts.message,fd_forum_posts.images from fd_forum_threads left join fd_forum_posts on 
            fd_forum_threads.tid=fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.invisible=0 and
             fd_forum_posts.isfirst=1 and fd_forum_threads.tid=".$tid;
            $list = $DB_1->query($sql);
            // print_r($list); die;
            $share['title'] = $list[0]['subject'];
            $share['desc'] = mb_substr($list[0]['message'], 0,20,'utf-8')."...";
            $share['link'] = "";
            $share['imgurl'] = $this->_get_one_share_img($list[0]['images']);

            //对分享进行记录
            $data['tid'] = $tid;
            $data['userid'] = $userid;
            $data['ctime'] = I('server.REQUEST_TIME');
            $DB_2->data($data)->add();

            //主题的分享次数+1
            $DB_1->where(array('tid' => $tid))->setInc('sharetimes');

            //分享成功后 关注用户加分
            $this->credit_system('share',$userid);

            if(!empty($share)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($share);
                $res['content'] = $share;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        
        echo json_encode($share);
    }



    /**
     * 根据帖子images获取第一张图片作为分享略缩图
     */
    private function _get_one_share_img($images){

        $posts_img_arr = explode(',', $images);
        $DB = M('forum_images');
        
        $imgurl = $DB->where(array('id' => $posts_img_arr[0]))->getField('url');

        if(!empty($imgurl)){

            return $imgurl;
        }else{

            return $imgurl = "";
        }
    }



    /**
      举报帖子或者主题 genre=1举报主题  genre=2举报帖子
     */
    
    public function set_tipoff_record(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $DB_1 = M('forum_threads_tipoff_record');
            $DB_2 = M('forum_posts_tipoff_record');
            $genre = I('get.genre');
            $userid = I('get.userid');
            $type = I('get.type');

            if($genre == 1){
                //举报主题
                $data['userid'] = $userid;
                $data['result'] = 0;
                $data['tid'] = I('get.tid');
                $data['type'] = $type;
                $data['ctime'] = I('server.REQUEST_TIME');

                $result = $DB_1->data($data)->add();

            }else if($genre == 2){
                //举报帖子
                $data['userid'] = $userid;
                $data['result'] = 0;
                $data['pid'] = I('get.pid');
                $data['type'] = $type;
                $data['ctime'] = I('server.REQUEST_TIME');

                $result = $DB_2->data($data)->add();
            }

            if(!empty($result)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = 0;
                $res['content'] = "";
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        echo json_encode($res);
    }


    /**
     * 主题的阅读记录添加
     */
    private function _add_threads_read_record($tid,$userid){

        $DB_1 = M('forum_threads_read_record');
        $DB_2 = M('forum_threads');

        $data['tid'] = $tid;
        $data['userid'] = $userid;
        $data['ctime'] = I('server.REQUEST_TIME');
        //添加主题阅读记录
        $DB_1->data($data)->add();
        //主题的阅读次数+1
        $DB_2->where(array('tid' => $tid))->setInc('views');
    }


    /**
     * 帖子评论记录添加
     */
    private function _add_posts_comment_record($pid,$userid){

        $DB_1 = M('forum_posts_comment_record');
        $DB_2 = M('forum_posts');
        // replies
        
        $data['pid'] = $pid;
        $data['userid'] = $userid;
        $data['ctime'] = I('server.REQUEST_TIME');
        //添加帖子评论记录
        $DB_1->data($data)->add();
        //帖子的评论次数+1
        $DB_2->where(array('pid' => $pid))->setInc('replies');
    }


    /**
     * 主题评论记录添加
     */
    private function _add_threads_comment_record($tid,$userid){

        $DB_1 = M('forum_threads_comment_record');
        $DB_2 = M('forum_threads');
        // replies
        
        $data['pid'] = $pid;
        $data['userid'] = $userid;
        $data['ctime'] = I('server.REQUEST_TIME');
        //添加主题评论记录
        $DB_1->data($data)->add();
        //主题的评论次数+1
        $DB_2->where(array('tid' => $tid))->setInc('replies');
    }


    /**
     * 圈子阅读记录添加
     */
    private function _add_read_forum_record($fid,$userid){

        $DB_1 = M('forum_read_forum_record');
        $DB_2 = M('forum_forum');

        $data['fid'] = $fid;
        $data['userid'] = $userid;
        $data['ctime'] = I('server.REQUEST_TIME');
        //添加主题评论记录
        $DB_1->data($data)->add();
        //主题的评论次数+1
        $DB_2->where(array('fid' => $fid))->setInc('views');
    }



    /**
        用户发表的帖子
     */
    public function get_user_published_posts(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('get.userid');
            $pagecount = I('get.pagecount');
            $pagenum = $pagecount * 10;
            $DB_1 = M('forum_users');
            //获取用户的头像用户的昵称
            $list = $DB_1->where(array('userid' => $userid))->field('avatar,nickname')->find();

            //获取用户发布的所有主题帖子
            $sql = "select fd_forum_threads.tid,fd_forum_threads.subject,fd_forum_threads.liketimes,fd_forum_threads.replies,
            fd_forum_threads.ctime,fd_forum_threads.issticktop,fd_forum_threads.isdigest,fd_forum_threads.ishot,
            fd_forum_threads.isthumb,fd_forum_threads.ishighlight,fd_forum_threads.isspecial,fd_forum_posts.message from 
            fd_forum_threads left join fd_forum_posts on 
            fd_forum_threads.tid=fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.isfirst=1 
            and fd_forum_posts.invisible=0 and fd_forum_threads.userid=".$userid." order by fd_forum_threads.ctime desc limit
             ".$pagenum.",10";

            $list['threads'] = $DB_1->query($sql);
            
            foreach ($list['threads'] as $key => $value) {
              # code...
                $list['threads'][$key]['message'] = mb_substr($value['message'],0,99,'utf-8');
            }

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);

    }



    /**
        通过用户mid将用户资料插入forumusers数据表
        并返回用户的userid
     */
    
    public function insert_forumuser_by_mid($mid){

        $mid = $mid;

        $DB_1 = M('members');
        $DB_2 = M('forum_users');

        //查询用户是否已经存在
        $reback = $DB_2->where(array('mid' => $mid))->find();

        if(!empty($reback)){
            //如果用户已经存在
            $userid = $reback['userid'];
        }else{
            //用户不存在
            //从members获取用户的资料
            $list = $DB_1->where(array('id' => $mid))->field('id,username,avatar')->find();

            $data['ctime'] = I('server.REQUEST_TIME');
            $data['mid'] = $list['id'];
            $data['nickname'] = $list['username'];
            $data['avatar'] = $list['avatar'];
            //把资料添加到forum_users表
            $userid = $DB_2->data($data)->add();
        }

        if(!empty($userid)){

            return $userid;
        }
    }



    /**
        获取用我的个人中心里的信息资料
     */
    public function get_forum_user_info($userid){

        //得到用户等级粉丝数，关注数，积分
        $DB_1 = M('forum_users');
        $list = $DB_1->where(array('userid' => $userid))->field('level,followedcount,followcount,credit')->find();
        //获取用户发帖数量
        $list['threadscount'] = M('forum_threads')->where(array('userid' => $userid,'invisible' => 0))->count();
        //获取用户的回帖数量
        $list['repliescount'] = M('forum_posts')->where(array('userid' => $userid,'isfirst' => 0,'invisible' => 0))->count();
        //获取收藏帖子的数量
        $list['liketimes'] = M('forum_threads_collection_record')->where(array('userid' =>$userid,'iscollection' => 1))->count();
        //获取最新浏览的帖
        $time = time() - 3600*24*7;
        $map['userid'] = $userid;
        $map['ctime'] = array('gt',$time);
        $list['readscount'] = M('forum_threads_read_record')->where($map)->group('tid')->count();
        //查看用户是否是管理员
        $reback = M('forum_manager_relationship')->where(array('userid' => $userid))->find();
        if(!empty($reback)){

            $list['is_manager'] = 1;
        }else{

            $list['is_manager'] = 0;
        }

    }


    /**
     * 敏感词过滤
     */
    private function chk_bad_words($content,$type=1){

        $DB = M('bad_words_filter');

        $list = $DB->where(array('type' => $type))->getField('keywords',true);

        foreach ($list as $key => $value) {
            # code...

            $arr = explode($value,$content);
            if(count($arr)>1){

                $res['err'] = 1;
                $res['msg'] = '含有敏感词';
                $res['count'] = 0;
                $res['content']= "";

                echo json_encode($res);
                die;

            }else{

            }
        }

        return;

    }



    /**
     * 积分系统
     */
    /**
     * [credit_system description]
     * @return [type] [description]
     */
    private function credit_system($taskname,$userid){

        $DB_1 = M('forum_credit_task');
        $DB_2 = M('forum_credit_record');
        $DB_3 = M('forum_users');
        $DB_4 = M('forum_credit_level');

        //首先获取该任务操作应获得积分
        $list = $DB_1->where(array('taskname' => $taskname))->find();

        //对用户加分操作进行检测是否超过限制次数
        $reback = $this->chk_user_get_credit_times($list,$userid);
        if(!$reback){

            return;
        }

        //获取该用户现有的积分和累计积分
        $data = $DB_3->where(array('userid' => $userid))->field('credit,credit_fix')->find();

        //将用户应获得积分加入现有积分
        $data['credit'] = $data['credit'] + $list['task_credit'];
        $data['credit_fix'] = $data['credit_fix'] + $list['task_credit'];

        //重新计算用户得分扣分后的等级状态
        $sql = "select fd_forum_credit_level.level_id,fd_forum_credit_level.min_credit as min_num,
        fd_forum_credit_level.max_credit as max_num from fd_forum_credit_level having min_num<=".$data['credit_fix'].
        " && max_num>".$data['credit_fix'];

        $list2 = $DB_4->query($sql);
        
        //将用户的等级更新到用户表
        $data['level_id'] = $list2[0]['level_id'];
        $res = $DB_3->where(array('userid' => $userid))->data($data)->save();
        //用户数据更新成功后写入记录
        if(!empty($res)){
            $ctime = I('server.REQUEST_TIME');
            $DB_2->data(array('userid' => $userid,'taskid' => $list['id'],'ctime' => $ctime))->add();
        }
        return;
    }





    /**
     * 检测用户获得积分测次数是否合法
     */
    private function chk_user_get_credit_times($list,$userid){

        $DB = M('forum_credit_record');

        if($list['type'] == 1){
            //永久性任务
            $map['taskid'] = $list['id'];
            $map['userid'] = $userid;
            //获取用户已参与次数
            $count = $DB->where($map)->count();
            if($count >= $list['count']){
                //如果用户参与次数大于或者等于规则次数
                return FALSE;
            }else{

                return TRUE;
            }

        }else if($list['type'] == 2){
            //每日性任务
            $map['userid'] = $userid;
            $map['taskid'] = $list['id'];
            $now_time = date('Y-m-d',time());
            $time = strtotime($now_time);
            $map['ctime'] = array('gt',$time);
            //获取用户今天已参与次数
            $count = $DB->where($map)->count();
            if($count >= $list['count']){
                //如果用户今日参与次数已超过限制次数
                return FALSE;
            }else{

                return TRUE;
            }
        }
    }



    /**
      获取我的发表的帖子列表
     */
    public function get_my_threads_lists(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('get.userid');
            $pagecount = I('get.pagecount');
            $pagenum = $pagecount * 10;
            $DB_1 = M('forum_users');
            //获取用户的头像用户的昵称
            $list = $DB_1->where(array('userid' => $userid))->field('avatar,nickname')->find();

            //获取用户发布的所有主题帖子
            $sql = "select fd_forum_threads.tid,fd_forum_threads.subject,fd_forum_threads.liketimes,fd_forum_threads.replies,
            fd_forum_threads.ctime,fd_forum_threads.issticktop,fd_forum_threads.isdigest,fd_forum_threads.ishot,
            fd_forum_threads.isthumb,fd_forum_threads.ishighlight,fd_forum_threads.isspecial,fd_forum_posts.message from 
            fd_forum_threads left join fd_forum_posts on 
            fd_forum_threads.tid=fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.isfirst=1 
            and fd_forum_posts.invisible=0 and fd_forum_threads.userid=".$userid." order by fd_forum_threads.ctime desc limit
             ".$pagenum.",10";

            $list['threads'] = $DB_1->query($sql);
            
            //截取帖子前99个字作为简介
            foreach ($list['threads'] as $key => $value) {
              # code...
                $list['threads'][$key]['message'] = mb_substr($value['message'],0,99,'utf-8');
            }

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);
    }


    /**
        获取我回复的帖子列表
     */
    public function get_my_posts_lists(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);
        // var_dump($chk_user); die;
        if($chk_user){

            $userid = I('get.userid');
            $pagecount = I('get.pagecount');
            $pagenum = $pagecount * 10;
            $DB_2 = M('forum_posts');
            $DB_3 = M('forum_threads');


            //获取用户参与的所有帖子的tid
            $list = $DB_2->where(array('isfirst' => 0,'invisible' => 0))->group('tid')->getField('tid',true);

            //循环获取帖子主题内容
            foreach ($list as $key => $value) {
                
                $sql = "select fd_forum_threads.tid,fd_forum_threads.userid,fd_forum_threads.subject,fd_forum_threads.issticktop,
                fd_forum_threads.isdigest,fd_forum_threads.ishot,fd_forum_threads.isthumb,fd_forum_threads.ishighlight,
                fd_forum_threads.isspecial,fd_forum_threads.liketimes,fd_forum_threads.replies,fd_forum_posts.message,
                fd_forum_users.nickname,fd_forum_users.avatar from fd_forum_threads left join fd_forum_users on 
                fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on fd_forum_threads.tid=
                fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0
                 and fd_forum_users.invisible=0 and fd_forum_threads.tid=".$value." order by fd_forum_threads.ctime desc 
                 limit ".$pagenum.",10";

                $list2[$key] = $DB_3->query($sql);
            }
            
            //截取帖子前99个字作为简介
            foreach ($list2 as $key => $value) {
              # code...
                $list3[$key] = $value[0];
                $list3[$key]['message'] = mb_substr($value[0]['message'],0,99,'utf-8');
            }

            if(!empty($list3)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list3);
                $res['content'] = $list3;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);
    }



    /**
        获取我收藏的帖子列表
     */
    public function get_my_collection_lists(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);
        // var_dump($chk_user); die;
        if($chk_user){

            $userid = I('get.userid');
            $pagecount = I('get.pagecount');
            $pagenum = $pagecount * 10;
            $DB_2 = M('forum_threads_collection_record');
            $DB_3 = M('forum_threads');


            //获取用户收藏的所有帖子的tid
            $list = $DB_2->where(array('userid' => $userid,'iscollection' => 1))->getField('tid',true);
            // print_r($list); die;
            //循环获取帖子主题内容
            foreach ($list as $key => $value) {
                
                $sql = "select fd_forum_threads.tid,fd_forum_threads.userid,fd_forum_threads.subject,fd_forum_threads.issticktop,
                fd_forum_threads.isdigest,fd_forum_threads.ishot,fd_forum_threads.isthumb,fd_forum_threads.ishighlight,
                fd_forum_threads.isspecial,fd_forum_threads.liketimes,fd_forum_threads.replies,fd_forum_posts.message,
                fd_forum_users.nickname,fd_forum_users.avatar from fd_forum_threads left join fd_forum_users on 
                fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on fd_forum_threads.tid=
                fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0
                 and fd_forum_users.invisible=0 and fd_forum_threads.tid=".$value." order by fd_forum_threads.ctime desc 
                 limit ".$pagenum.",10";

                $list2[$key] = $DB_3->query($sql);
            }
            // print_r($list2); die;
            
            //截取帖子前99个字作为简介
            foreach ($list2 as $key => $value) {
              # code...
                $list3[$key] = $value[0];
                $list3[$key]['message'] = mb_substr($value[0]['message'],0,99,'utf-8');
            }

            if(!empty($list3)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list3);
                $res['content'] = $list3;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);
    }



    /**
        获取最近浏览的帖子列表
     */
    public function get_my_readthreads_lists(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);
        // var_dump($chk_user); die;
        if($chk_user){

            $userid = I('get.userid');
            $pagecount = I('get.pagecount');
            $pagenum = $pagecount * 10;
            $DB_2 = M('forum_threads_read_record');
            $DB_3 = M('forum_threads');

            $time = time() - 3600*24*7;
            $map['userid'] = $userid;
            $map['ctime'] = array('gt',$time);
            //获取用户浏览的所有帖子的tid
            $list = $DB_2->where($map)->group('tid')->getField('tid',true);
            // print_r($list); die;
            //循环获取帖子主题内容
            foreach ($list as $key => $value) {
                
                $sql = "select fd_forum_threads.tid,fd_forum_threads.userid,fd_forum_threads.subject,fd_forum_threads.issticktop,
                fd_forum_threads.isdigest,fd_forum_threads.ishot,fd_forum_threads.isthumb,fd_forum_threads.ishighlight,
                fd_forum_threads.isspecial,fd_forum_threads.liketimes,fd_forum_threads.replies,fd_forum_posts.message,
                fd_forum_users.nickname,fd_forum_users.avatar from fd_forum_threads left join fd_forum_users on 
                fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on fd_forum_threads.tid=
                fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.isfirst=1 and fd_forum_posts.invisible=0
                 and fd_forum_users.invisible=0 and fd_forum_threads.tid=".$value." order by fd_forum_threads.ctime desc 
                 limit ".$pagenum.",10";

                $list2[$key] = $DB_3->query($sql);
            }
            // print_r($list2); die;
            
            //截取帖子前99个字作为简介
            foreach ($list2 as $key => $value) {
              # code...
                $list3[$key] = $value[0];
                $list3[$key]['message'] = mb_substr($value[0]['message'],0,99,'utf-8');
            }

            if(!empty($list3)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list3);
                $res['content'] = $list3;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);
    }



    /**
        查一查帖子搜索功能
     */
    public function search_threads_by_keywords(){

        $mid = I('get.mid');
        $password=I('get.password');
        $chk_user = $this->chk_user($mid, $password);

        if($chk_user){

            $userid = I('get.userid');
            $keywords = I('get.keywords');
            $pagecount = I('get.pagecount');
            $pagenum = $pagecount * 10;
            $DB_1 = M('forum_threads');
            $DB_2 = M('forum_users');

            //获取用户发布的所有主题帖子
            $sql = "select fd_forum_users.avatar,fd_forum_users.nickname,fd_forum_threads.tid,fd_forum_threads.subject,
            fd_forum_threads.liketimes,fd_forum_threads.replies,
            fd_forum_threads.ctime,fd_forum_threads.issticktop,fd_forum_threads.isdigest,fd_forum_threads.ishot,
            fd_forum_threads.isthumb,fd_forum_threads.ishighlight,fd_forum_threads.isspecial,fd_forum_posts.message from 
            fd_forum_threads left join fd_forum_users on fd_forum_threads.userid=fd_forum_users.userid left join fd_forum_posts on 
            fd_forum_threads.tid=fd_forum_posts.tid where fd_forum_threads.invisible=0 and fd_forum_posts.isfirst=1 
            and fd_forum_posts.invisible=0 and fd_forum_threads.subject like '%".$keywords."%' order by fd_forum_threads.ctime desc limit
             ".$pagenum.",10";

            $list = $DB_1->query($sql);
            // print_r($list); die;
            //截取帖子前99个字作为简介
            //获取发表用户的头像昵称
            foreach ($list as $key => $value) {
              # code...
                $list[$key]['message'] = mb_substr($value['message'],0,99,'utf-8');
                $list[$key]['avatar'] = $DB_2->where(array('userid' => $value['userid']))->getField('avatar');
                $list[$key]['nickname'] = $DB_2->where(array('userid' => $value['userid']))->getField('nickname');
            }

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['count'] = count($list);
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'error';
                $res['count'] = 0;
                $res['content'] = "";
            }

        }else{

            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            $res['count'] = 0;
            $res['content']= "";
        }

        

        echo json_encode($res);
    }
    

}